package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.GroupNoticeTitlePics;
import cn.stylefeng.guns.sys.modular.system.mapper.GroupNoticeTitlePicsMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeTitlePicsParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeTitlePicsResult;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeTitlePicsService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 群通知封面表 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-07-26
 */
@Service
public class GroupNoticeTitlePicsServiceImpl extends ServiceImpl<GroupNoticeTitlePicsMapper, GroupNoticeTitlePics> implements GroupNoticeTitlePicsService {

    @Override
    public GroupNoticeTitlePics add(GroupNoticeTitlePicsParam param) {
        GroupNoticeTitlePics entity = getEntity(param);
        this.save(entity);
        return entity;
    }

    @Override
    public ResponseData delete(GroupNoticeTitlePicsParam param) {
        //TODO-1 逻辑删除
        GroupNoticeTitlePics entity = getEntity(param);
        if (!param.getPortalUserId().equals(entity.getPortalUserId())) {
            return ResponseData.error(412, "删除失败，当前封面图片不是本人上传，只可删除自己上传的封面");
        }
        entity.setDelFlag(1);
        this.baseMapper.updateById(entity);
        return ResponseData.success();
    }

    @Override
    public void update(GroupNoticeTitlePicsParam param) {
        GroupNoticeTitlePics oldEntity = getOldEntity(param);
        GroupNoticeTitlePics newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GroupNoticeTitlePicsResult findBySpec(GroupNoticeTitlePicsParam param) {
        return null;
    }

    @Override
    public List<GroupNoticeTitlePicsResult> findListBySpec(GroupNoticeTitlePicsParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GroupNoticeTitlePicsParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<GroupNoticeTitlePicsResult> findListByParam(GroupNoticeTitlePicsParam param) {

        List<GroupNoticeTitlePicsResult> resultList = this.baseMapper.findListByParam(param);
        return resultList;
    }

    private Serializable getKey(GroupNoticeTitlePicsParam param) {
        return param.getTitlePicId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GroupNoticeTitlePics getOldEntity(GroupNoticeTitlePicsParam param) {
        return this.getById(getKey(param));
    }

    private GroupNoticeTitlePics getEntity(GroupNoticeTitlePicsParam param) {
        GroupNoticeTitlePics entity = new GroupNoticeTitlePics();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
